import Model from "./Model";
import Config from "./Config";
import WxUtil from "./WxUtil";

let wx = window['wx']
const { ccclass, property } = cc._decorator;

@ccclass
export default class Menu extends cc.Component {
    @property(cc.Label)
    draw_label: cc.Label = null;

    @property(cc.Node)
    draw_Complete: cc.Node = null;

    @property(cc.Label)
    drop_label: cc.Label = null;

    @property(cc.Node)
    drop_Complete: cc.Node = null;

    @property(cc.Label)
    drag_label: cc.Label = null;

    @property(cc.Node)
    drag_Complete: cc.Node = null;

    @property(cc.Label)
    prcise_label: cc.Label = null;

    @property(cc.Node)
    price_Complete: cc.Node = null;

    // LIFE-CYCLE CALLBACKS:
    @property(cc.Node)
    wxSubContextView: cc.Node = null;


    @property({ type: cc.AudioClip })
    bg: cc.AudioClip = null;

    static hasCreatAuthorBtn: boolean = false;
    onLoad() {
        Config.init()

        this.draw_label.getComponent(cc.Label).string = (Model.game.drawLevel - 1) + '/' + (Config.drawLevels.length - 1)
        this.drop_label.getComponent(cc.Label).string = (Model.game.dropLevel - 1) + '/' + (Config.brickLevels.length - 1)
        this.drag_label.getComponent(cc.Label).string = (Model.game.dragLevel - 1) + '/' + (Config.dragLevels.length - 1)
        this.prcise_label.getComponent(cc.Label).string = (Model.game.preciseLevel - 1) + '/' + (Config.preciseLevels.length - 1)

        this.price_Complete.active = Model.game.completeInfo[0] == 1
        this.drop_Complete.active = Model.game.completeInfo[1] == 1
        this.drag_Complete.active = Model.game.completeInfo[2] == 1
        this.draw_Complete.active = Model.game.completeInfo[3] == 1

        if (!cc.audioEngine.isMusicPlaying())
            cc.audioEngine.playMusic(this.bg, true);

        WxUtil.showShareMenu()
        WxUtil.showBanner()
    }

    onLevelUIClick(e) {
        Model.game.selectedModel = 4
        cc.director.loadScene('levelUI')
    }
    onPreciseClick(e) {
        Model.game.selectedModel = 1
        if (Model.game.preciseLevel > Config.preciseLevels.length - 1) {
            Model.game.preciseLevel = 1
        }
        Model.game.selectedLevel = Model.game.preciseLevel
        cc.director.loadScene('game')
    }
    onDropClick(e) {
        Model.game.selectedModel = 2
        if (Model.game.dropLevel > Config.brickLevels.length - 1) {
            Model.game.dropLevel = 1
        }
        Model.game.selectedLevel = Model.game.dropLevel
        cc.director.loadScene('gameDrop')
    }
    onDragClick(e) {
        if (Model.game.dragLevel > Config.dragLevels.length - 1) {
            Model.game.dragLevel = 1
        }
        Model.game.selectedModel = 3
        Model.game.selectedLevel = Model.game.dragLevel
        cc.director.loadScene('gameDrag')
    }
    onDrawClick(e) {
        Model.game.selectedModel = 4
        if (Model.game.drawLevel <= Config.drawLevels.length - 1) {
            Model.game.selectedLevel = Model.game.drawLevel
            cc.director.loadScene('gameDraw')
        } else {
            cc.director.loadScene('levelUI')
        }

    }
    onRankClick(e) {
        if (typeof wx === 'undefined') {
            return;
        }

        this.initUserInfoButton()

        if (!this.wxSubContextView.active) {
            // 设置容器可见
            this.wxSubContextView.active = true;


            // 发送结算分数到开放域
            wx.getOpenDataContext().postMessage({
                message: 'show'
            });
            console.log('显示排行榜')
        }
        else {
            // 设置容器不可见，即关闭排行榜，并让开放域清空排名信息
            this.wxSubContextView.active = false;
            wx.getOpenDataContext().postMessage({
                message: 'clear'
            });
            console.log('关闭排行榜')
        }
    }
    initUserInfoButton() {
        // 微信授权，此代码来自Cocos官方
        if (typeof wx === 'undefined') {
            return;
        }
        if (Menu.hasCreatAuthorBtn) {
            return;
        }

        let systemInfo = wx.getSystemInfoSync();
        let width = systemInfo.windowWidth;
        let height = systemInfo.windowHeight;
        let button = wx.createUserInfoButton({
            type: 'text',
            text: '',
            style: {
                left: 0,
                top: 0,
                width: width,
                height: height,
                lineHeight: 40,
                backgroundColor: '#00000000',
                color: '#00000000',
                textAlign: 'center',
                fontSize: 10,
                borderRadius: 4
            }
        });

        if (Menu.hasCreatAuthorBtn == false) {
            Menu.hasCreatAuthorBtn = true
        }

        button.onTap((res) => {
            if (res.userInfo) {
                // 可以在这里获取当前玩家的个人信息，如头像、微信名等。
                console.log('授权成功！');
            }
            else {
                console.log('授权失败！');
            }

            button.hide();
            button.destroy();
        });
    }
    onShareClick(e) {
        WxUtil.share()
    }
    onCloseRank(e) {
        this.wxSubContextView.active = false;
        wx.getOpenDataContext().postMessage({
            message: 'clear'
        });
        console.log('关闭排行榜')
    }
    onFeedBackClick(e) {

    }
    onDestroy() {
        console.log('销毁menu')
        WxUtil.destroyBanner()
    }
}
